perlfandomcom-20200213-history
Named colors
Here is a table that shows all the named colors recognized by modern browsers and X11 systems. The name is printed in both black and white to help you decide which font color to choose. Colors.pl The table above was written by a simple perl script called colors.pl and a plain text file that lists all 139 named colors, each on its own line with no spaces: AliceBlue AntiqueWhite Aqua Aquamarine Azure Beige Bisque Black BlanchedAlmond Blue BlueViolet Brown BurlyWood CadetBlue Chartreuse Chocolate Coral CornflowerBlue Cornsilk Crimson Cyan DarkBlue DarkCyan DarkGoldenrod DarkGray DarkGreen DarkKhaki DarkMagenta DarkOliveGreen DarkOrange DarkOrchid DarkRed DarkSalmon DarkSeaGreen DarkSlateBlue DarkSlateGray DarkTurquoise DarkViolet DeepPink DeepSkyBlue DimGray DodgerBlue FireBrick FloralWhite ForestGreen Fuchsia Gainsboro GhostWhite Gold Goldenrod Gray Green GreenYellow Honeydew HotPink IndianRed Indigo Ivory Khaki Lavender LavenderBlush LawnGreen LemonChiffon LightBlue LightCoral LightCyan LightGoldenrodYellow LightGreen LightGrey LightPink LightSalmon LightSeaGreen LightSkyBlue LightSlateGray LightSteelBlue LightYellow Lime LimeGreen Linen Magenta Maroon MediumAquamarine MediumBlue MediumOrchid MediumPurple MediumSeaGreen MediumSlateBlue MediumSpringGreen MediumTurquoise MediumVioletRed MidnightBlue MistyRose Moccasin NavajoWhite Navy OldLace Olive OliveDrab Orange OrangeRed Orchid PaleGoldenrod PaleGreen PaleTurquoise PaleVioletRed PapayaWhip PeachPuff Peru Pink Plum PowderBlue Purple Red RosyBrown RoyalBlue SaddleBrown Salmon SandyBrown SeaGreen Seashell Sienna Silver SkyBlue SlateBlue SlateGray Snow SpringGreen SteelBlue Tan Teal Thistle Tomato Turquoise Violet Wheat White WhiteSmoke Yellow YellowGreen If you want to play with these colors, copy the list and save it in your working directory as NamedColors.txt Now, in that same directory, make a new file called colors.pl or whatever you like. Start with the usual opening line: #!/usr/bin/perl -w The '-w' switch tells your compiler to 'w'arn you of errors in your syntax. # colors.pl - a simple script for handling named colors. It's nice to add the script's filename and a brief description of what it does. open (COLORS, "NamedColors.txt") or die; @Colors=; close COLORS; We open (read-only) the bare list of NamedColors and slurp it into an array, @Colors. We do this via a filehandle called . Then we close the unaltered file. Now we use a foreach loop statement to break each $color out of @Colors and wrap it in wiki syntax for tables, stuffing each color into a new array, @NamedColors: foreach $color (@Colors) { chomp$color; @NamedColors=("@NamedColors", "\n|- style=\"background:$color\"", "\n|style=\"color:black\"|$color", "\n|style=\"color:white\"|$color") } Notice that the '"'s (double quotes) for the style attributes in the wiki syntax must be escaped with '\'s (backslashes). The colors.pl script produces wiki table syntax foreach color appending @NamedColors with: |- style="background:AliceBlue" |style="color:black"|AliceBlue |style="color:white"|AliceBlue When the loop has iterated the whole list, we now use a new file handle COLORTABLE to hold and print the newly composed table rows and cells: open (COLORTABLE, ">ColorTable.txt") or die; print {COLORTABLE} "@NamedColors"; close COLORTABLE; The open statement this time is write-enabled (>). The colors.pl script has now created a new file in your working directory – ColorTable.txt You can add a little confirmation message at the end: print "\n A file: ColorTable.txt has been written to your working directory\n"; To run the script make sure you have NamedColors.txt in your current working directory - the same directory from which you run colors.pl. In a terminal, you type: perl colors.pl Now you can open ColorTable.txt and enclose it with and . Once you get the hang of it, you can make all sorts of nifty tools for manipulating colors with wiki syntax and perl scripts. Category:Tutorial Your turn Too advanced? See Scripting. Too simple? See POE or help with CPAN! Problems, questions, ideas, comments? Let us know! Category:Tutorial